﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using MaxLeaf.DB;
using System.Data.Common;
using System.Data;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using System.Transactions;
using System.Collections;


namespace MaxLeaf.DAL.Web2SCP
{
    public class OeSalesOrderDAL
    {
        public void Delete(DBAccess dbWebRead, DBAccess dbWebWrite, DBAccess dbCon, string strKeyValue)
        {
            DBAccess dbSCPBase = dbCon;            
            DBAccess dbOrd = new DBAccess(dbCon.SCPSEOrderDB);
            DBAccess dbFinterDB = new DBAccess(dbCon.SCPFinterIDDB);
            try
            {
                string strSQL = "";
                dbOrd.BeginTrans();
                //strSQL = "Delete From poOrderRecvHdr where FinterID ='" + strOldFinterID + "' and companyid='" + strCompanyID + "'";
                //dbOrd.ExecuteSql(strSQL);
                //strSQL = "Delete From poOrderRecvDtl where HdrID ='" + strOldFinterID + "' and companyid='" + strCompanyID + "'";
                //dbOrd.ExecuteSql(strSQL);
                strSQL = "select * from order_main where order_ID = " + strKeyValue + "";
                DataRow drWebOrder = dbWebRead.GetDataFirstRow(strSQL);
                if (drWebOrder != null)
                {
                    string strCompanyID = CvtFunc.ToString(dbSCPBase.GetValue("select companyid from mCompany where WebUserID='" + drWebOrder["User_ID"] + "'"));

                    strSQL = "select FinterID from poOrderRecvHdr where WebOrdNo='" + drWebOrder["Order_sn"] + "'";
                    string strOldFinterID = CvtFunc.ToString(dbOrd.GetValue(strSQL));

                    strSQL = "UPDATE poOrderRecvHdr set is_delete=1 where  FinterID ='" + strOldFinterID + "' and companyid='" + strCompanyID + "'";
                    dbOrd.ExecuteSql(strSQL);
                    strSQL = "UPDATE poOrderRecvDtl set is_delete=1 where  HdrID ='" + strOldFinterID + "' and companyid='" + strCompanyID + "'";
                    dbOrd.ExecuteSql(strSQL);
                }
                dbOrd.CommitTrans(); 
            }
            catch (Exception ex)
            {
                dbOrd.RollbackTrans();
                throw new MaxLeafException("添加出库单失败！");    
            }
        }
        public void Add(DBAccess dbWebRead, DBAccess dbWebWrite, DBAccess dbCon, string strKeyValue)
        {
            DBAccess dbSCPBase = dbCon;            
            DBAccess dbOrd = new DBAccess(dbCon.SCPSEOrderDB);
            DBAccess dbFinterDB = new DBAccess(dbCon.SCPFinterIDDB);
            try
            {
                dbOrd.BeginTrans();
                                
                #region 加表头
                DataRow drHdrNew = dbOrd.GetDataTable("Select top 0 * from poOrderRecvHdr").NewRow();
                string strFinterID = "";
                string strSQL = "select * from order_main where order_ID = " + strKeyValue + "";
                DataRow drWebOrder = dbWebRead.GetDataFirstRow(strSQL);
                if (drWebOrder != null)
                {
                    string strCompanyID = "";

                    //如果不是签约客户
                    if (CvtFunc.ToLong( dbSCPBase.GetValue( "Select WebUserID from mCompany where WebUserID='" + drWebOrder["User_ID"] + "'")) <= 0)
                    {
                        return;
                    }

                    if (CvtFunc.ToDouble(dbSCPBase.GetValue("Select WebUserID from mCompany where WebUserID='" + drWebOrder["son_id"] + "'")) > 0)
                    {
                        strCompanyID = CvtFunc.ToString(dbSCPBase.GetValue("select companyid from mCompany where WebUserID='" + drWebOrder["son_id"] + "'"));
                    }                    
                    else if (CvtFunc.ToLong( dbSCPBase.GetValue( "Select WebUserID from mCompany where WebUserID='" + drWebOrder["User_ID"] + "'")) > 0)
                    {
                        strCompanyID = CvtFunc.ToString(dbSCPBase.GetValue("select companyid from mCompany where WebUserID='" + drWebOrder["User_ID"] + "'"));
                    }
                    if (strCompanyID == "")
                    {
                        dbCon.RollbackTrans(); 
                        return;
                    }
                    //如果有就要删除原来的单据
                    strSQL = "select FinterID from poOrderRecvHdr where WebOrdNo='" + drWebOrder["Order_sn"] + "'";
                    string strOldFinterID = CvtFunc.ToString ( dbOrd.GetValue(strSQL)) ;
                    if (CvtFunc.ToLong(strOldFinterID) > 0)
                    {
                        //strSQL = "Delete From poOrderRecvHdr where FinterID ='" + strOldFinterID + "' and companyid='"+ strCompanyID +"'";
                        //dbOrd.ExecuteSql(strSQL);
                        //strSQL = "Delete From poOrderRecvDtl where HdrID ='" + strOldFinterID + "' and companyid='"+ strCompanyID +"'";
                        //dbOrd.ExecuteSql(strSQL);

                        strSQL = "UPDATE poOrderRecvHdr set is_delete=1 where  FinterID ='" + strOldFinterID + "' and companyid='" + strCompanyID + "'";
                        dbOrd.ExecuteSql(strSQL);
                        strSQL = "UPDATE poOrderRecvDtl set is_delete=1 where  HdrID ='" + strOldFinterID + "' and companyid='" + strCompanyID + "'";
                        dbOrd.ExecuteSql(strSQL);
                    }

                    strFinterID = dbFinterDB.GetValue("exec GetICFinteID @IsSave,@FBillType,@BillId output"
                           , new SqlParameter[] {
                        new SqlParameter("@IsSave", "1")
                        ,new SqlParameter("@FBillType","27")
                        ,new SqlParameter("@BillID",SqlDbType.VarChar,50)
                    }
                           , "BillID");
                    drHdrNew["FInterID"] = strFinterID;
                    drHdrNew["CompanyId"] = strCompanyID;
                    drHdrNew["WebOrdNo"] = CvtFunc.ToString(drWebOrder["Order_sn"]);
                    drHdrNew["OrdDate"] = CvtFunc.ToDateTime( CvtFunc.ToLong ( drWebOrder["order_time"]));
                    drHdrNew["Phone"] = CvtFunc.ToString(drWebOrder["tel"]);
                    drHdrNew["Mobile"] = CvtFunc.ToString(drWebOrder["Mobile"]);
                    drHdrNew["Address"] = CvtFunc.ToString(drWebOrder["address"]);
                    drHdrNew["PostalCode"] = CvtFunc.ToString(drWebOrder["zip"]);
                    drHdrNew["MDCId"] = CvtFunc.ToInt(drWebOrder["MDC_ID"]);
                    drHdrNew["TotAmt"] = CvtFunc.ToDouble(drWebOrder["order_amount"]);
                    drHdrNew["OrdStatus"] = CvtFunc.ToString(drWebOrder["order_status"]);
                    int intRecvUserId = CvtFunc.ToInt(dbSCPBase.GetValue("select UserId from mUser_main where WebUserID='" + drWebOrder["User_ID"] + "'"));
                    drHdrNew["RecvUserId"] = intRecvUserId;
                    KeyValuePair<string, IDataParameter[]> kvp = dbOrd.CreateParamSql("RecvUserId,FInterID,CompanyId,WebOrdNo,OrdDate,Phone,Mobile,Address,PostalCode,MDCId,TotAmt,OrdStatus", "poOrderRecvHdr", drHdrNew, LogStatus.Add, "finterid");
                    dbOrd.ExecuteSql(kvp.Key, kvp.Value);
                }
                #endregion

                strSQL = "select * from order_goods where order_ID = " + strKeyValue + "";
                using (DataTable dtWebOrder = dbWebRead.GetDataTable(strSQL))
                {
                    foreach (DataRow drWebOrder_Dtl in dtWebOrder.Rows)
                    {
                        DataRow drDtlNew = dbOrd.GetDataTable("Select top 0 * from poOrderRecvDtl").NewRow();

                        string strFinterID_Dtl = dbFinterDB.GetValue("exec GetICFinteID @IsSave,@FBillType,@BillId output"
                                              , new SqlParameter[] {
                                                    new SqlParameter("@IsSave", "1")
                                                    ,new SqlParameter("@FBillType","28")
                                                    ,new SqlParameter("@BillID",SqlDbType.VarChar,50)
                                                }
                        , "BillID");

                        drDtlNew["DtlID"] = strFinterID_Dtl;
                        drDtlNew["CompanyId"] = drHdrNew["CompanyId"];
                        drDtlNew["Cost"] = CvtFunc.ToDouble(drWebOrder_Dtl["goods_price"]);
                        drDtlNew["HdrID"] = strFinterID;
                        strSQL = "SELECT B.unit_group_id FROM goods_measure_unit B where  B.measure_unit_id='" + drWebOrder_Dtl["order_unit_id"] + "'";
                        string strUnit_group_ID = dbSCPBase.GetValue(strSQL);
                        string strgoods_id = CvtFunc.ToString(drWebOrder_Dtl["goods_id"]);
                        string strattr_id = CvtFunc.ToString(drWebOrder_Dtl["goods_attr_id"]);

//                        strSQL = @"  select distinct isnull(b.goods_attr_id,0)
//                                    from dbo.goods_main a
//                                    left join dbo.goods_attribute b on a.goods_id=b.goods_id
//                                    left join dbo.goods_measure_unit c on a.unit_group_id=c.unit_group_id 
//                                    where a.goods_id=@goods_id and b.attr_id=@attr_id and c.unit_group_id=@unit_group_id";

                        //string strGoods_attr_id = dbSCPBase.GetValue(strSQL, new SqlParameter[]{ 
                        //                               new SqlParameter("@goods_id",strgoods_id )
                        //                            ,  new SqlParameter("@attr_id", strattr_id )
                        //                            , new SqlParameter( "@unit_group_id" , strUnit_group_ID )});

                        string strGoods_attr_id = strattr_id;

                        drDtlNew["ItemId"] = CvtFunc.ToInt(dbSCPBase.GetValue("Select finterid from iminvmaster where GoodsID=@GoodsID and GoodsAttrID=@GoodsAttrID",
                                                          new SqlParameter[]{ new SqlParameter( "@GoodsID", strgoods_id )
                                                                              , new SqlParameter ("@GoodsAttrID", strGoods_attr_id)}));
                        drDtlNew["ReqQty"] = CvtFunc.ToDouble(drWebOrder_Dtl["order_unit_number"]);
                        drDtlNew["UnitId"] = CvtFunc.ToInt(drWebOrder_Dtl["order_unit_id"]);
                        drDtlNew["WebPrice"] = CvtFunc.ToDouble(drWebOrder_Dtl["goods_price"]);
                        drDtlNew["SubTotal"] = CvtFunc.ToDouble(drWebOrder_Dtl["goods_amount"]);
                        KeyValuePair<string, IDataParameter[]> kvp = dbOrd.CreateParamSql("DtlID,HdrID,CompanyId,Cost,ItemId,ReqQty,UnitId,WebPrice,SubTotal", "poOrderRecvDtl", drDtlNew, LogStatus.Add, "DtlID");
                        dbOrd.ExecuteSql(kvp.Key, kvp.Value);
                    }
                }
                dbOrd.CommitTrans(); 
            }
            catch (Exception ex)
            {
                dbOrd.RollbackTrans();
                throw new MaxLeafException("添加出库单失败！");                
            }
        }
    }
}
